package com.fzj.ivos_serve.user.controller;

import com.fzj.ivos_serve.base.IPUtil;
import com.fzj.ivos_serve.base.response.JsonResult;
import com.fzj.ivos_serve.user.pojo.dto.UserLoginParam;
import com.fzj.ivos_serve.user.pojo.dto.UserQuery;
import com.fzj.ivos_serve.user.pojo.dto.UserSaveParam;
import com.fzj.ivos_serve.user.pojo.vo.UserVO;
import com.fzj.ivos_serve.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/v1/user")
@RestController//@ResponseBody+@Controller
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public JsonResult login(UserLoginParam userLoginParam, HttpServletRequest request){
        String ipAddr = IPUtil.getIpAddr(request);
        String cityInfo = IPUtil.getCityInfo(ipAddr);
        log.info("ip,cityInfo===>{},{}",ipAddr,cityInfo);
        log.debug("用户登录，参数：{}",userLoginParam);
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }
    @GetMapping("/select")
    public JsonResult select(UserQuery userQuery){
        log.debug("查询用户，参数：{}",userQuery);
        List<UserVO> userVOList = userService.selectUserList(userQuery);
        return JsonResult.ok(userVOList);
    }
    @PostMapping("/save")
    public JsonResult save(UserSaveParam userSaveParam){
        log.debug("保存用户信息，参数：{}",userSaveParam);
        userService.save(userSaveParam);
        return JsonResult.ok();
    }
    @GetMapping("/selectById/{id}")
    public JsonResult selectById(@PathVariable Long id){
        log.debug("根据id查询用户，参数：{}",id);
        UserVO userVO = userService.selectById(id);
        return JsonResult.ok(userVO);
    }
    @PostMapping("/deleteById/{id}")
    public JsonResult deleteById(@PathVariable Long id){
        log.debug("根据id删除用户，参数：{}",id);
        userService.deleteById(id);
        return JsonResult.ok();
    }
    @PostMapping("/updateStatus/{id}/{status}")
    public JsonResult updateStatus(@PathVariable Long id ,@PathVariable String status){
        log.debug("修改状态，参数：{}，{}",id,status);
        userService.updateStatus(id,status);
        return JsonResult.ok();
    }
    @PostMapping("resetPassword/{id}")
    public JsonResult resetPassword(@PathVariable Long id){
        log.debug("重置密码，参数：{}",id);
        userService.resetPassword(id);
        return JsonResult.ok();
    }
}

